package com.yd.cfckc.controller;

import com.yd.cfckc.util.RedisPool;
import com.yd.cfckc.util.SrvUtil;
import com.yd.cfckc.util.StrFun;
import com.yd.cfckc.util.XmlFun;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author cuiyufeng
 * @email cuiyufeng1458@yundasys.com
 * @date 2020/10/16 1:45 下午
 */
@Slf4j
@RestController
@RequestMapping("/cfckc")
public class CfckcController {

    @RequestMapping(value = "/cfckc.jsp",method = RequestMethod.POST,produces = {"application/xml; charset=UTF-8"})
    public String getCfckc(HttpServletRequest request){
        StringBuffer result = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        BufferedReader in = null;
        try {
             in = request.getReader();
        } catch (IOException e) {
            e.printStackTrace();
        }
        char[] buf;
        int contentLength = request.getContentLength();
        if (contentLength > 0) {
            buf = new char[contentLength];
        } else {
            buf = new char[4 * 1024];
        }
        int readLength;

        try {
            while ((readLength = in.read(buf, 0, buf.length)) > 0) {
                sb.append(buf, 0, readLength);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        String xml = sb.toString();
        log.info("----------------------------------");
        //log.info("巴枪发送报文:" + xml);
        // ======================================================================
        // 2 解析数据
        String[] aTags = new String[0];
        try {
            aTags = XmlFun.getTags(xml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String op = aTags[0];
        String ver = aTags[1];
        String comp = aTags[2].replace(" ", "");
        String user = aTags[3].replace(" ", "");
        String pass = aTags[4];
        String dev1 = aTags[5];
        String dev2 = aTags[6];
        JedisPool pool = RedisPool.getPool();
        Jedis jedis = null;

        //时间
        long now = System.currentTimeMillis();
        String current = StrFun.getTime("yyyy-MM-dd HH:mm:ss");
        String compCode = SrvUtil.getProperties().getProperty("compCode");
        log.info("分拨: " + compCode + "\n请求时间: " + current);

        try {
            // 缺少请求指令
            if (op.equals("")) {
                result.append("<dta st=\"er\" ercode=\"1\"></dta>");
                return result.toString();
            } else if (!compCode.equals(comp)) {//请求分拨编码不符合
                result.append("<dta st=\"er\" ercode=\"2\"></dta>");
                return result.toString();
            } else if (op.equals("getcfcheck")) {// 错分校验
                if (ver.equals("2.0")) {
                    jedis = pool.getResource();
                    String[] sendData = XmlFun.getTagAllValue(xml, "d");
                    String data = "";
                    String mailno = sendData[0];
                    if (sendData != null && !"".equals(mailno)) {
                        log.info("校验单号: " + mailno);
                        String value = jedis.get(mailno);
                        log.info("巴枪校验值: " + sendData[1]);
                        if (jedis.exists(mailno)) {
                            log.info("系统实际值: " + value);
                            log.info("----------------------------------");
                            if (value != null && !"".equals(value)) {
                                String arr[] = value.split("@");
                                String sjdw = jedis.get(sendData[1]);
                                if (Arrays.asList(arr).contains(sendData[1]) || Arrays.asList(arr).contains(sjdw)) {
                                    data = "<data><o><d>1</d><d>校验成功</d></o></data>";
                                } else {
                                    data = "<data><o><d>2</d><d>校验错误</d></o></data>";
                                }
                            } else {
                                data = "<data><o><d>5</d><d>无集包地信息</d></o></data>";
                            }
                        } else {
                            data = "<data><o><d>3</d><d>没有数据</d></o></data>";
                        }
                    } else {
                        data = "<data><o><d>4</d><d>数据解析错误</d></o></data>";
                    }
                    String res = "<dta st=\"ok\" op=\"getcfcheck\" >\n<h><ver>2.0</ver><time>" + current
                            + "</time></h>\n" + data + "\n</dta>";
                    return res;
                }
            } else if (op.equals("getcfckmsg")) {// 获取错分校验预测信息
                if (ver.equals("2.0")) {
                    jedis = pool.getResource();
                    String[] sendData = XmlFun.getTagAllValue(xml, "d");
                    String data = "";
                    String mailno = sendData[0];
                    if (sendData != null && !"".equals(mailno)) {
                        log.info("校验单号: " + mailno);
                        String value = jedis.get(mailno);
                        log.info("巴枪校验值: " + sendData[1]);
                        if (jedis.exists(mailno)) {
                            data = "<data><o><d>" + value + "</d></o></data>";
                        } else {
                            data = "<data><o><d>没有数据(系统无此单号信息)</d></o></data>";
                        }
                    } else {
                        data = "<data><o><d>4</d><d>数据解析错误</d></o></data>";
                    }
                    String res = "<dta st=\"ok\" op=\"getcfckmsg\" >\n<h><ver>2.0</ver><time>" + current
                            + "</time></h>\n" + data + "\n</dta>";
                    return res;
                }
            } else {//请求指令错误
                result.append("<dta st=\"er\" ercode=\"3\"></dta>");
               return result.toString();
            }
        } catch (Exception e) {
            String s = e.getMessage();
            pool.returnBrokenResource(jedis);
            log.error(s);
            return s;
        } finally {
            pool.returnResource(jedis);
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
